package us.mooresr.geartrack.client.core.desktop;

import java.util.List;

import us.mooresr.geartrack.client.core.presenter.ItemTypePresenter;
import us.mooresr.geartrack.client.core.presenter.ItemTypePresenter.MyView;
import us.mooresr.geartrack.client.core.presenter.ItemTypeUiHandlers;
import us.mooresr.geartrack.shared.model.DatastoreObject;
import us.mooresr.geartrack.shared.model.ItemType;
import us.mooresr.geartrack.shared.model.Location;

import com.gwtplatform.mvp.client.ViewImpl;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class DesktopItemTypeViewImpl extends ViewWithUiHandlers<ItemTypeUiHandlers> 
	implements ItemTypePresenter.MyView, ContentWidgetEventHandler {

	private final Widget widget;
	private final ContentWidget contentWidget;
	private  SingleItemForm singleItemForm;
	private ItemType selectedItemType;
	@UiField HTMLPanel contentPanel;

	public interface Binder extends UiBinder<Widget, DesktopItemTypeViewImpl> {
	}

	@Inject
	public DesktopItemTypeViewImpl(final Binder binder) {
		widget = binder.createAndBindUi(this);
		contentWidget = GWT.create(ContentWidget.class);
		contentWidget.setTitle("Item Types");
		contentPanel.add(contentWidget);
		contentWidget.setHandler(this);
		
		singleItemForm = new SingleItemForm();
		contentWidget.setContent(singleItemForm);
		
		// Assume we start with no item selected and an empty form, so disable the save and delete buttons.
		clearSelection();
	}
	
	private void clearSelection() {
		selectedItemType = null;
		contentWidget.clearSelection();
		contentWidget.disableButton(1);
		contentWidget.disableButton(2);
	}

	@Override
	public Widget asWidget() {
		return widget;
	}

	@Override
	public void onSelection(DatastoreObject selectedObject) {
		selectedItemType = (ItemType)selectedObject;
		
		singleItemForm.setName(selectedItemType.getName());

		
		contentWidget.enableButton(1);
		contentWidget.enableButton(2);
		
	}

	@Override
	public void onButton1Click() {
		// Save Button

		String name = singleItemForm.getName();
		if ((name == null) || (name.length() == 0)) {
			// Must have a name
			Window.alert("Name can not be blank.");
			return;
		}

		// End of validation
		selectedItemType.setName(singleItemForm.getName());
		

		getUiHandlers().updateItemType(selectedItemType);
		
		singleItemForm.clear();
		
	}

	@Override
	public void onButton2Click() {
		// Delete Button
		if (selectedItemType == null) {
			return;
		}
		
		String name = selectedItemType.getName();
		String message = "Are you sure you want to delete " + name + "?";
		boolean answer = Window.confirm(message);
		
		if (answer) {
			getUiHandlers().deleteItemType(selectedItemType);
			singleItemForm.clear();
		}
		
		
	}

	@Override
	public void onButton3Click() {
		// Add Button

		String name = singleItemForm.getName();
		if ((name == null) || (name.length() == 0)) {
			// Must have a name
			Window.alert("Name can not be blank when adding a new location.");
			return;
		}

		// End of validation
		ItemType newItemType = new ItemType();
		newItemType.setName(name);

		getUiHandlers().addItemType(newItemType);
		
		singleItemForm.clear();
		
	}

	@Override
	public void setItemTypeList(List<ItemType> list) {
		contentWidget.setListContents(list);
		clearSelection();
		
	}

	@Override
	public void addItemTypeToList(ItemType itemType) {
		contentWidget.addItemToList(itemType);
		
		
	}

	@Override
	public void removeItemTypeFromList(ItemType itemType) {
		contentWidget.removeItemFromList(itemType);
		
	}

	@Override
	public void updateListEntry(ItemType itemType) {
		contentWidget.updateListItem(itemType);
		
	}
}
